$(function () {
    obj.init();
    console.log('aa');
});
var obj = new function () {
    "use strict";
    return {
        cuId: null,
        schoolId:null,
        dataGrid: null,
        dom: {
            addBtn: null,
            delBtn: null,
            search: null,
            default1: null,
            bAccount: null
        },
        init: function () {
            this.cuId = iCUId;
            this.schoolId = xhTool.getQuery('schoolId');
            this.dataGrid = new xhDataGrid($('#List'), 'school_contact/getContactData?id=' + this.cuId, {});
            this.dataGrid.parseToolbar().init().activeButton(['bt_list_user_group'], ['bt_list_del','bt_setDefault', 'bt_list_bindAccount']);
            this.dom.addBtn = $('#bt_list_add');
            this.dom.delBtn = $('#bt_list_del');
            this.dom.search = $('#bt_list_search');
            this.dom.bAccount = $('#bt_list_bindAccount');

            this.listener();

        },
        listener: function () {
            var $this = this;

            //新建联系人
            $this.dom.addBtn.click(function () {
                $this.funObj().openAddContact();
            });
            //删除联系人
            $this.dom.delBtn.click(function () {
                if (xhExtra.buttonDisabled($(this))) {
                    return false;
                }
                $this.funObj().openDelView();

            });
            //绑定账号
            $this.dom.bAccount.click(function () {
                if (xhExtra.buttonDisabled($(this))) {
                    return false;
                }
                $this.funObj().bindAccount();
            });

            //设置用户组
            $('#bt_list_user_group').click(function() {
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().openUserGroup();
            });

            //搜索
            $this.dom.search.click(function () {
                $this.funObj().search();
            });
            //绑定dataGrid监听事件
            this.funObj().bindDgEvent();

        },
        funObj: function () {
            var $this = this;
            return {
                openAddContact: function () {
                    window.top.xhTop.showModal({
                        title: '新建联系人',
                        content: '<iframe src="' + baseUrl + 'school_contact/contactForm?icuId=' + $this.cuId + ' " style="height: 300px;border-style: none;padding: 0px;width: 100%"></iframe>',
                        button: '提交',
                        width:850,
                        backClose: false
                    });
                },
                openDelView: function () {
                    var id = $this.dataGrid.getSelectValue('iCUCId');
                    xhExtra.askChangeStatus('school_contact/delContact',{id:id},null,null,function () {
                        $this.dataGrid.reload();
                        window.top.xhTop.showSuccess(res.msg);
                    });
                },
                search: function () {
                    var key = xhExtra.getValue($('#list_search'));
                    $this.dataGrid.addDataGridParams({key: key});
                },
                bindAccount: function () {
                    var iCUCId = $this.dataGrid.getSelectValue('iCUCId');
                    var sName  = $this.dataGrid.getSelectValue('sName');
                    var sPhoneNum =  $this.dataGrid.getSelectValue('sPhoneNum');
                    window.top.xhTop.showModal({
                        title: '绑定账号',
                        content: '<iframe src="' + baseUrl + 'school_contact/bindAccount?iCUCId=' + iCUCId + '&sName='+escape(sName)+'&sPhoneNum='+sPhoneNum+'&schoolId='+$this.schoolId+' " style="height: 300px;border-style: none;padding: 0px;width: 100%;"></iframe>',
                        button : '新建账号',
                        size   : 'lg',
                        backClose: false
                    });
                },
                bindDgEvent: function () {
                    $this.dataGrid.onDblClickRow = function (rowIndex, rowData) {
                        window.top.xhTop.showModal({
                            title: '编辑联系人',
                            content: '<iframe src="' + baseUrl + 'school_contact/contactForm?icuId=' + $this.cuId + '&icucId=' + rowData['iCUCId'] + '" style="height: 300px;border-style: none;padding: 0px;width: 100%;"></iframe>',
                            button: '提交',
                            width:850,
                            backClose: false
                        });
                    };
                },
                openUserGroup: function () {
                    var userIds = $this.dataGrid.getSelectValues('iUserId');
                    window.top.xhTop.showModal({
                        title: '设置用户组(共' + userIds.length + '人)',
                        button: '确认',
                        content: xhTool.getFrameString('Group/userGroup', {type: 3, userIds: userIds.join(',')}, 500)
                    });
                }
            }
        }

    }
};


